const GroupBy = require("../template")

function testGroupByBasic() {
  console.log(
    "1. 基本分组功能测试:",
    (() => {
      const array = [
        { name: 'Alice', age: 25, city: 'New York' },
        { name: 'Bob', age: 30, city: 'London' },
        { name: 'Charlie', age: 25, city: 'Paris' },
        { name: 'David', age: 30, city: 'Tokyo' }
      ]
      const result = GroupBy.groupBy(array, 'age')
      return Object.keys(result).length === 2 && result['25'].length === 2 && result['30'].length === 2
    })()
  )

  console.log(
    "2. 函数分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result).length === 2 && result['even'].length === 5 && result['odd'].length === 5
    })()
  )

  console.log(
    "3. 深度分组测试:",
    (() => {
      const array = [
        { user: { name: 'Alice', age: 25 }, city: 'New York' },
        { user: { name: 'Bob', age: 30 }, city: 'London' },
        { user: { name: 'Charlie', age: 25 }, city: 'Paris' }
      ]
      const result = GroupBy.groupByDeep(array, 'user.age', 2)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "4. 多条件分组测试:",
    (() => {
      const array = [
        { name: 'Alice', age: 25, city: 'New York' },
        { name: 'Bob', age: 30, city: 'London' },
        { name: 'Charlie', age: 25, city: 'Paris' },
        { name: 'David', age: 30, city: 'Tokyo' }
      ]
      const result = GroupBy.groupByMultiple(array, 'age', 'city')
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "5. 条件过滤分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithFilter(array, x => x % 2 === 0 ? 'even' : 'odd', x => x > 5)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "6. 映射分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithMap(array, x => x % 2 === 0 ? 'even' : 'odd', x => x * 2)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "7. 排序分组测试:",
    (() => {
      const array = [3, 1, 2, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithSort(array, x => x % 2 === 0 ? 'even' : 'odd', (a, b) => a - b)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "8. 统计分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithStats(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result.result).length === 2 && typeof result.stats === 'object'
    })()
  )

  console.log(
    "9. 转换分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithTransform(array, x => x % 2 === 0 ? 'even' : 'odd', {
        even: arr => arr.map(x => x * 2),
        odd: arr => arr.map(x => x * 3)
      })
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "10. 验证分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithValidation(array, x => x % 2 === 0 ? 'even' : 'odd', { min: 1, max: 10 })
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "11. 空数组分组测试:",
    (() => {
      const array = []
      const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result).length === 0
    })()
  )

  console.log(
    "12. 单元素数组分组测试:",
    (() => {
      const array = [1]
      const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result).length === 1 && result['odd'].length === 1
    })()
  )

  console.log(
    "13. this上下文分组测试:",
    (() => {
      const groupByObj = {
        multiplier: 2,
        processGroupBy: function(array) {
          const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
          return Object.keys(result).length * this.multiplier
        }
      }
      const array = [1, 2, 3, 4]
      return groupByObj.processGroupBy(array) === 4 // 2*2=4
    })()
  )

  console.log(
    "14. 复杂对象this绑定分组测试:",
    (() => {
      const groupByConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(array) {
          const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
          return Object.keys(result).length >= this.config.threshold
        }
      }
      const array = [1, 2, 3, 4]
      return groupByConfigObj.processWithConfig(array) === true
    })()
  )
}

testGroupByBasic()
